from django.db import models
import uuid
from datetime import datetime, timedelta
class Users(models.Model):
    id = models.UUIDField(
        primary_key=True,
        default=uuid.uuid4,
        editable=False
    )
    email = models.EmailField(max_length=254, unique=True, blank=False, null=False)
    username = models.CharField(max_length=50)
    password_hash = models.CharField(max_length=128)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    is_active = models.BooleanField(
        default=True,
        verbose_name='激活状态',
        help_text='表示用户是否被视为活跃用户。设为False可以代替删除账户。'
    )
    def __str__(self):
        return {
            'username': self.username,
            'email': self.email,
            'password_hash': self.password_hash,
            'created_at': self.created_at,
            'updated_at': self.updated_at,
            'is_active': self.is_active,
        }

class CustomToken(models.Model):
    user = models.OneToOneField(Users, on_delete=models.CASCADE)  # 关联用户
    key = models.CharField(max_length=255, unique=True, default=uuid.uuid4)  # Token 字符串
    created_at = models.DateTimeField(auto_now_add=True)  # 生成时间
    expired_at = models.DateTimeField(default=datetime.now() + timedelta(hours=24))  # 过期时间，这里设为24小时后

    def __str__(self):
        return str(self.key)